
const {ccclass, property} = cc._decorator;
import supModule from "../../../sup/supModule";
import supMount from "../../../sup/supMount";
import ruleModule from "./ruleModule";
import settingModule from "./settingModule";

class rebargs {
    ds:number = null;//下滑时间
    us:number = null;//回弹时间
    dc:number = null;//回弹距离
    constructor(args:number[]){
        this.dc = args.pop();
        this.us = args.pop();
        this.ds = args.pop();
    }
}


@ccclass
export default class jymtGameTabMount extends supMount {
    @property({type:cc.Button,tooltip:"开启settings"})
    pulldown: cc.Button     = null;
    @property({type:cc.Button,tooltip:"关闭settings"})
    pullup: cc.Button       = null;
    @property({type:cc.Node,tooltip:"settings节点"})
    settings: cc.Node       = null;
    @property({type:cc.Node,tooltip:"加速中按钮节点"})
    speedbgstart: cc.Node   = null;
    @property({type:cc.Node,tooltip:"未加速按钮节点"})
    speedbgclose: cc.Node   = null;
    @property({type:cc.Node,tooltip:"转动按钮节点"})
    spinstart: cc.Node      = null;
    @property({type:cc.Node,tooltip:"停止按钮节点"})
    spinclose: cc.Node      = null;
    @property({type:cc.Node,tooltip:"开始位置节点"})
    iconstart: cc.Node[]    = [];
    @property({type:cc.Node,tooltip:"结束位置节点"})
    iconclose: cc.Node[]    = [];
    @property({type:cc.Node,tooltip:"结果位置节点"})
    layouticons: cc.Node[]   = [];
    @property({type:cc.Node,tooltip:"开始位置节点"})
    layoutstart: cc.Node    = null;
    @property({type:cc.Node,tooltip:"结束位置节点"})
    layoutclose: cc.Node    = null;
    @property({type:cc.Node,tooltip:"结果显示节点"})
    layoutshows: cc.Node    = null;
    @property({type:cc.Node,tooltip:"自动旋转选择"})
    spinautopan: cc.Node    = null;
    @property({type:cc.Float,tooltip:"[0]下滑时间[1]回弹时间[2]回弹距离",multiline:true})
    rebound:number[]        = [];


    private game:supModule = null;
    private wnet:cMessageModule = this.getModule("wsmoduleNetwork");
    private jetton:number  = 1;//下注选项
    private freect:number  = 0;//免费次数
    private autoct:number  = 0;//自动旋转
    private spintm:number  = 0;//点击时间
    private spinps:number  = 1100;//长按时间
    private atimer:any     = null;//自动定时
    private rebarg:rebargs = null;//回弹参数

    extendStart(){
        //初始化
        this.rebarg = new rebargs(this.rebound);
        this.rebarg = new rebargs(this.rebound);

        //异步处理
        setTimeout(function(){
            let game = this.getModule("sc_jymt_game");
            game.uistart(this);
            this.game = game;
        }.bind(this),0);

        //绑定事件
        this.spinstart.on(cc.Node.EventType.MOUSE_DOWN,()=>{
            let node = this.spinstart
            if(node.active){
                this.spintm = Date.now();
                this.atimer = setTimeout(() => {
                    if(node.active){
                        let auto = this.spinautopan;
                        if(!auto.active){
                            auto.active = true;
                            auto.scaleY = 0;
                            let anim = cc.scaleTo(0.5,1,1);
                            auto.runAction(anim);
                        }
                    }
                }, this.spinps);
            }
            
        },this);

        //绑定事件
        this.spinstart.on(cc.Node.EventType.MOUSE_UP,function(){
            this.spintg = false;
            this.spintm = Date.now();
            clearTimeout(this.atimer);
        },this);
    }

    //settings打开
    ondownsetting(){
        this.pulldown.node.active = false;
        this.pullup.node.active = true;
        this.settings.active = true;
    }
    //settings关闭
    onupstting(){
        this.pulldown.node.active = true;
        this.pullup.node.active = false;
        this.settings.active = false;
    }

    //返回大厅
    onreturnlobby(){
        this.startScript("scmoduleviews",ruleModule);
    }

    //帮助界面
    onhelppancel(){
        this.startScript("sc_jymt_help",ruleModule);
    }

    //记录界面
    onrecordpancel(){
        
    }

    //设置界面
    onsettingpancel(){
        this.startScript("sc_jymt_setting",settingModule);
    }

    //重转按钮
    onresetAxle(...grgs:any[]){
        let v:string = grgs.pop();
        let x:number = Number(v);
        // this.wnet.sendmail("jymt_xpin",[cEnumJYMT.table,cEnumJYMT.xpin],{
        //     axle:x,
        // });
    }

    //下注按钮
    onchangebet(){
    }

    //开启加速
    onspeedspinstart(){
        this.speedbgstart.active = false;
        this.speedbgclose.active = true;
    }

    //关闭加速
    onspeedspinclose(){
        this.speedbgstart.active = true;
        this.speedbgclose.active = false;
    }

    //启动旋转
    onroratespinstart(){
        this.spinstart.active = false;
        this.spinclose.active = true;
        let pass = Date.now() - this.spintm;
        if(pass < this.spinps){
            // this.wnet.sendmail("jymt_spin",[cEnumJYMT.table,cEnumJYMT.xpin],{
            //     index:this.jetton
            // });
        }
    }

    //停止旋转
    onroratespinclose(){
        this.spinstart.active = true;
        this.spinclose.active = false;
        let auto = this.spinautopan
        if(auto.active){
            auto.active = true;
            let anim = cc.scaleTo(0.5,1,0);
            let call = cc.callFunc(()=>{
                auto.active = false;
            });
            let anis = cc.sequence(anim,call);
            auto.runAction(anis);
        }
    }

    private _startVec2:Map<number,any> = new Map();
    //获取起点位置
    startVec2(x:number):cc.Vec2{
        let map = this._startVec2;
        let index = x-1;
        if(!map[index]){
            let node = this.iconstart[index];
            map[index]=this.layoutstart.convertToWorldSpace(node.position);
        }
        return map[index]
    }

    private _closeVec2:Map<number,any> = new Map();
    //获取终点位置
    closeVec2(x:number):cc.Vec2{
        let map = this._closeVec2;
        let index = x-1;
        if(!map[index]){
            let node = this.iconclose[index];
            map[index]=this.layoutclose.convertToWorldSpace(node.position);
        }
        return map[index]
    }

    private _resultVec2:Map<number,any> = new Map();
    //获取结果位置
    public resultVec2(pos:number):cc.Vec2{
        let map = this._resultVec2;
        let index = pos-1;
        if(!map[index]){
            let node = this.layouticons[index];
            map[index]=this.layoutshows.convertToWorldSpace(node.position);
        }
        return map[index]
    }
    //向量相减
    public vec2Subtract(a:cc.Vec2,b:cc.Vec2):cc.Vec2{
        return new cc.Vec2(a.x-b.x,a.y-b.y);
    }

    //向量相加
    public vec2Adding(a:cc.Vec2,b:cc.Vec2):cc.Vec2{
        return new cc.Vec2(a.x+b.x,a.y+b.y);
    }
    
    //世界坐标转结果局部坐标
    public wvec2Tresult(v:cc.Vec2):cc.Vec2{
        return this.layoutshows.convertToNodeSpace(v);
    }

    private _vec2resetBy:Map<number,any> = new Map();
    //获取结果坐标
    public vec2resetBy(pos:number):cc.Vec2{
        let map = this._vec2resetBy
        if(!map[pos]){
            if(pos >= 1 &&　pos　<= 15){
                //展示区域
                let wpos = this.resultVec2(pos);
                map[pos] = this.wvec2Tresult(wpos);
            }
            else if(pos >= -4 &&　pos　<= 0){
                //起点区域
                let wpos = this.startVec2(pos+5);
                map[pos] = this.wvec2Tresult(wpos);
            }
            else if(pos >= 16 &&　pos　<= 20){
                //终点区域
                let wpos = this.closeVec2(pos-15);
                map[pos] = this.wvec2Tresult(wpos);
            }
        }
        return map[pos];
    }

    //抖动效果
    public rebountAnim():any{
        let arg = this.rebarg;
        let a = cc.moveBy(arg.ds,cc.v2(0,-arg.dc));
        let b = cc.moveBy(arg.us,cc.v2(0,arg.dc));
        let s = cc.sequence(a,b);
        return s;
    }

    public dispatchs(data:msgHead) {
        let cmd = data.cmds.pop();
        
    }

}
